/*
 * Copyright (c) 2023 vitasystems GmbH
 *
 * This file is part of project EHRbase Migration Tool
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ehrbase.migration.exporter.v0.jooq.pg.tables;

import java.sql.Timestamp;
import java.util.UUID;
import org.ehrbase.migration.exporter.v0.jooq.pg.Ehr;
import org.ehrbase.migration.exporter.v0.jooq.pg.tables.records.CompExpandRecord;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.JSONB;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Schema;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.TableOptions;
import org.jooq.impl.DSL;
import org.jooq.impl.SQLDataType;
import org.jooq.impl.TableImpl;

/**
 * This class is generated by jOOQ.
 */
@SuppressWarnings({"all", "unchecked", "rawtypes"})
public class CompExpand extends TableImpl<CompExpandRecord> {

    private static final long serialVersionUID = 1L;

    /**
     * The reference instance of <code>ehr.comp_expand</code>
     */
    public static final CompExpand COMP_EXPAND = new CompExpand();

    /**
     * The class holding records for this type
     */
    @Override
    public Class<CompExpandRecord> getRecordType() {
        return CompExpandRecord.class;
    }

    /**
     * The column <code>ehr.comp_expand.ehr_id</code>.
     */
    public final TableField<CompExpandRecord, UUID> EHR_ID =
            createField(DSL.name("ehr_id"), SQLDataType.UUID, this, "");

    /**
     * The column <code>ehr.comp_expand.subject_externalref_id_value</code>.
     */
    public final TableField<CompExpandRecord, String> SUBJECT_EXTERNALREF_ID_VALUE =
            createField(DSL.name("subject_externalref_id_value"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.comp_expand.subject_externalref_id_namespace</code>.
     */
    public final TableField<CompExpandRecord, String> SUBJECT_EXTERNALREF_ID_NAMESPACE =
            createField(DSL.name("subject_externalref_id_namespace"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.comp_expand.composition_id</code>.
     */
    public final TableField<CompExpandRecord, UUID> COMPOSITION_ID =
            createField(DSL.name("composition_id"), SQLDataType.UUID, this, "");

    /**
     * The column <code>ehr.comp_expand.template_id</code>.
     */
    public final TableField<CompExpandRecord, String> TEMPLATE_ID =
            createField(DSL.name("template_id"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.comp_expand.archetype_id</code>.
     */
    public final TableField<CompExpandRecord, String> ARCHETYPE_ID =
            createField(DSL.name("archetype_id"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.comp_expand.entry</code>.
     */
    public final TableField<CompExpandRecord, JSONB> ENTRY =
            createField(DSL.name("entry"), SQLDataType.JSONB, this, "");

    /**
     * The column <code>ehr.comp_expand.composition_name</code>.
     */
    public final TableField<CompExpandRecord, String> COMPOSITION_NAME =
            createField(DSL.name("composition_name"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.comp_expand.language</code>.
     */
    public final TableField<CompExpandRecord, String> LANGUAGE =
            createField(DSL.name("language"), SQLDataType.VARCHAR(5), this, "");

    /**
     * The column <code>ehr.comp_expand.territory</code>.
     */
    public final TableField<CompExpandRecord, Integer> TERRITORY =
            createField(DSL.name("territory"), SQLDataType.INTEGER, this, "");

    /**
     * The column <code>ehr.comp_expand.start_time</code>.
     */
    public final TableField<CompExpandRecord, Timestamp> START_TIME =
            createField(DSL.name("start_time"), SQLDataType.TIMESTAMP(6), this, "");

    /**
     * The column <code>ehr.comp_expand.start_time_tzid</code>.
     */
    public final TableField<CompExpandRecord, String> START_TIME_TZID =
            createField(DSL.name("start_time_tzid"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.comp_expand.end_time</code>.
     */
    public final TableField<CompExpandRecord, Timestamp> END_TIME =
            createField(DSL.name("end_time"), SQLDataType.TIMESTAMP(6), this, "");

    /**
     * The column <code>ehr.comp_expand.end_time_tzid</code>.
     */
    public final TableField<CompExpandRecord, String> END_TIME_TZID =
            createField(DSL.name("end_time_tzid"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.comp_expand.other_context</code>.
     */
    public final TableField<CompExpandRecord, JSONB> OTHER_CONTEXT =
            createField(DSL.name("other_context"), SQLDataType.JSONB, this, "");

    /**
     * The column <code>ehr.comp_expand.ctx_location</code>.
     */
    public final TableField<CompExpandRecord, String> CTX_LOCATION =
            createField(DSL.name("ctx_location"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.comp_expand.facility_name</code>.
     */
    public final TableField<CompExpandRecord, String> FACILITY_NAME =
            createField(DSL.name("facility_name"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.comp_expand.facility_ref</code>.
     */
    public final TableField<CompExpandRecord, String> FACILITY_REF =
            createField(DSL.name("facility_ref"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.comp_expand.facility_scheme</code>.
     */
    public final TableField<CompExpandRecord, String> FACILITY_SCHEME =
            createField(DSL.name("facility_scheme"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.comp_expand.facility_namespace</code>.
     */
    public final TableField<CompExpandRecord, String> FACILITY_NAMESPACE =
            createField(DSL.name("facility_namespace"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.comp_expand.facility_type</code>.
     */
    public final TableField<CompExpandRecord, String> FACILITY_TYPE =
            createField(DSL.name("facility_type"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.comp_expand.composer_name</code>.
     */
    public final TableField<CompExpandRecord, String> COMPOSER_NAME =
            createField(DSL.name("composer_name"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.comp_expand.composer_ref</code>.
     */
    public final TableField<CompExpandRecord, String> COMPOSER_REF =
            createField(DSL.name("composer_ref"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.comp_expand.composer_scheme</code>.
     */
    public final TableField<CompExpandRecord, String> COMPOSER_SCHEME =
            createField(DSL.name("composer_scheme"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.comp_expand.composer_namespace</code>.
     */
    public final TableField<CompExpandRecord, String> COMPOSER_NAMESPACE =
            createField(DSL.name("composer_namespace"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.comp_expand.composer_type</code>.
     */
    public final TableField<CompExpandRecord, String> COMPOSER_TYPE =
            createField(DSL.name("composer_type"), SQLDataType.CLOB, this, "");

    private CompExpand(Name alias, Table<CompExpandRecord> aliased) {
        this(alias, aliased, null);
    }

    private CompExpand(Name alias, Table<CompExpandRecord> aliased, Field<?>[] parameters) {
        super(
                alias,
                null,
                aliased,
                parameters,
                DSL.comment(""),
                TableOptions.view(
                        """
        create view "comp_expand" as  SELECT ehr.id AS ehr_id,
         party.party_ref_value AS subject_externalref_id_value,
         party.party_ref_namespace AS subject_externalref_id_namespace,
         entry.composition_id,
         entry.template_id,
         entry.archetype_id,
         entry.entry,
         ltrim(rtrim((regexp_split_to_array(json_object_keys((entry.entry)::json), 'and name/value='::text))[2], ''']'::text), ''''::text) AS composition_name,
         compo.language,
         compo.territory,
         ctx.start_time,
         ctx.start_time_tzid,
         ctx.end_time,
         ctx.end_time_tzid,
         ctx.other_context,
         ctx.location AS ctx_location,
         fclty.name AS facility_name,
         fclty.party_ref_value AS facility_ref,
         fclty.party_ref_scheme AS facility_scheme,
         fclty.party_ref_namespace AS facility_namespace,
         fclty.party_ref_type AS facility_type,
         compr.name AS composer_name,
         compr.party_ref_value AS composer_ref,
         compr.party_ref_scheme AS composer_scheme,
         compr.party_ref_namespace AS composer_namespace,
         compr.party_ref_type AS composer_type
        FROM (((((((ehr.entry
          JOIN ehr.composition compo ON ((compo.id = entry.composition_id)))
          JOIN ehr.event_context ctx ON ((ctx.composition_id = entry.composition_id)))
          JOIN ehr.party_identified compr ON ((compo.composer = compr.id)))
          JOIN ehr.ehr ehr ON ((ehr.id = compo.ehr_id)))
          JOIN ehr.status status ON ((status.ehr_id = ehr.id)))
          LEFT JOIN ehr.party_identified party ON ((status.party = party.id)))
          LEFT JOIN ehr.party_identified fclty ON ((ctx.facility = fclty.id)));
        """));
    }

    /**
     * Create an aliased <code>ehr.comp_expand</code> table reference
     */
    public CompExpand(String alias) {
        this(DSL.name(alias), COMP_EXPAND);
    }

    /**
     * Create an aliased <code>ehr.comp_expand</code> table reference
     */
    public CompExpand(Name alias) {
        this(alias, COMP_EXPAND);
    }

    /**
     * Create a <code>ehr.comp_expand</code> table reference
     */
    public CompExpand() {
        this(DSL.name("comp_expand"), null);
    }

    public <O extends Record> CompExpand(Table<O> child, ForeignKey<O, CompExpandRecord> key) {
        super(child, key, COMP_EXPAND);
    }

    @Override
    public Schema getSchema() {
        return aliased() ? null : Ehr.EHR;
    }

    @Override
    public CompExpand as(String alias) {
        return new CompExpand(DSL.name(alias), this);
    }

    @Override
    public CompExpand as(Name alias) {
        return new CompExpand(alias, this);
    }

    @Override
    public CompExpand as(Table<?> alias) {
        return new CompExpand(alias.getQualifiedName(), this);
    }

    /**
     * Rename this table
     */
    @Override
    public CompExpand rename(String name) {
        return new CompExpand(DSL.name(name), null);
    }

    /**
     * Rename this table
     */
    @Override
    public CompExpand rename(Name name) {
        return new CompExpand(name, null);
    }

    /**
     * Rename this table
     */
    @Override
    public CompExpand rename(Table<?> name) {
        return new CompExpand(name.getQualifiedName(), null);
    }
}
